package com.jd.fruit.model.admin.shop;

import java.util.ArrayList;
import java.util.List;

import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 商品分类属性
 * 
 */
@SuppressWarnings("serial")
@Table("t_attribute")
public class Attribute extends BaseModel<Attribute> { 
	public static final Attribute dao = new Attribute();
	
	
	/**
	 * 保存分类属性
	 * @param role
	 * @return 
	 */
	public boolean save(Attribute attribute){
		try {
			attribute.set("id",StringUtil.getKey());
			return attribute.save(); 
		} catch (Exception e) {
			throw new RuntimeException("分类属性保存异常！", e);
		}
	}
	
	/**
	 * 根据所有分类属性
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Attribute> list(int pageNumber,int pageSize){
		List<Object> whereParams = new ArrayList<Object>();
		StringBuffer whereSql=new StringBuffer(" FROM `t_attribute`");
		return this.paginate(pageNumber,pageSize,"SELECT  * ",whereSql.toString(),whereParams.toArray());
	}	
	/**
	 * 根据分类ID删除所对应的分类值
	 * @param categoryId
	 * @return
	 */
	public int deleteByCategory(Long categoryId){
		String sql="DELETE FROM `t_attribute` WHERE `attr_category`=?";
		return Db.update(sql, categoryId); 
	}
	
	/**
	 * 根据分类ID查询出下属分类
	 * @param catid
	 * @return
	 */
	public List<Attribute> getAttrByCat(String catid){ 
		String sql="SELECT ta.* FROM `t_category_attribute` tca JOIN `t_attribute` ta ON tca.`attr_id`=ta.`id` WHERE `cat_id`='c32c759b22874e3988d7e99d14932a7b' GROUP BY attr_id ORDER BY ta.`attr_sort`";
		return this.find(sql);
	}
	
	/**
	 * 根据分类ID查询出下属分类
	 * @param catid
	 * @return
	 */
	public static List<Record> getValByAttr(String attrid){ 
		String sql="SELECT * FROM `t_category_attribute` WHERE `attr_id` =?";
		return Db.find(sql,attrid);
	}
	@Override
	public void initWhere() {
		// TODO Auto-generated method stub
		
	}
	
	
}
